package com.yfp.operation.service.impl;

import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.operation.domain.MealReplacementDO;
import com.yfp.operation.domain.dto.MealReplacementDTO;
import com.yfp.operation.domain.vo.MealReplacementVO;
import com.yfp.operation.mapper.MealReplacementMapper;
import com.yfp.operation.service.MealReplacementService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 * @description 针对表【meal_replacement(代取餐状态表)】的数据库操作Service实现
 * @createDate 2024-04-17 15:53:40
 */
@Service
public class MealReplacementServiceImpl extends ServiceImpl<MealReplacementMapper, MealReplacementDO> implements MealReplacementService {

    @Resource
    private MealReplacementMapper mealReplacementMapper;

    /**
     * 根据用户id和设备id查询代取餐状态
     *
     * @param machineId 设备id
     * @return
     */
    @Override
    public List<MealReplacementDO> getByMachineId(String machineId) {
        LambdaQueryWrapper<MealReplacementDO> query = Wrappers.lambdaQuery(MealReplacementDO.class);
        query.eq(MealReplacementDO::getMachineId, machineId);

        return this.list(query);
    }

    /**
     * 判断是否存在在线的客服
     *
     * @param machineId 设备id
     * @return
     */
    @Override
    public Boolean isOnlineCustomer(String machineId) {
        List<MealReplacementDO> mealReplacementDOS = this.getByMachineId(machineId);
        // 根据用户id分组，并且获取分组后按照时间排序的最后一条数据
        Map<Long, MealReplacementDO> collect = mealReplacementDOS.stream().collect(Collectors.toMap(MealReplacementDO::getUserId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(MealReplacementDO::getId))));
        // 状态存在1则返回true否则false
        return collect.values().stream().map(MealReplacementDO::getStatus).anyMatch(status -> status.equals(1));
    }

    @Override
    public void delOperationUser(Long operationUserId, List<String> machines) {
        LambdaQueryWrapper<MealReplacementDO> query = Wrappers.lambdaQuery(MealReplacementDO.class);
        query.eq(MealReplacementDO::getUserId, operationUserId);
        query.in(MealReplacementDO::getMachineId, machines);
        this.remove(query);
    }

    @Override
    public List<MealReplacementVO> searchList(MealReplacementDTO mealReplacementDTO) {

        List<MealReplacementVO> mealReplacementDOS = mealReplacementMapper.searchList(mealReplacementDTO);

        mealReplacementDOS.forEach(mealReplacementDO -> {
            if (Objects.nonNull(mealReplacementDO.getStartTime()) && Objects.nonNull(mealReplacementDO.getEndTime())){
                String s = DateUtil.formatBetween(mealReplacementDO.getStartTime(), mealReplacementDO.getEndTime(), BetweenFormatter.Level.SECOND);
                mealReplacementDO.setTotalTime(s);
            }
        });
        return mealReplacementDOS;
    }
}




